<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>


<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="net.elswear.PMF"%>
<%@page import="net.elswear.Page"%>
<%@page import="java.util.List"%>
<html>
  <body>

<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
<p>Hello, <%= user.getNickname() %>! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with greetings you post.</p>
<%
    }
%>
<%
    PersistenceManager pm = PMF.get().getPersistenceManager();
    String query = "select from " + Page.class.getName();
    List<Page> pages = (List<Page>) pm.newQuery(query).execute();
    if (pages.isEmpty()) {
%>
<p>The system has no pages.</p>
<%
    } else {
        for (Page p : pages) {
            if (p.getAuthor() == null) {
%>
<p>An anonymous person created:</p>
<%
            } else {
%>
<p><%= p.getAuthor().getNickname() %> created:</p>
<%
            }
%>
<p>Name: <%= p.getName() %></p>
<p>Title: <%= p.getTitle() %></p>
<p>Content: <%= p.getContent() %></p>
<%
        }
    }
    pm.close();
%>
<div><p>Add New Page</p></div>
    <form action="/admin" method="post">
      <div>Name: <input type="text" name="name"></input></div>
      <div>Title: <input type="text" name="title"></input></div>   
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="Insert Page" /></div>
    </form>
  
  </body>
</html>